package com.dg.cuckoo.admin.service.impl;

import com.dg.cuckoo.admin.dto.earlyProject.EarlyProjectDto;
import com.dg.cuckoo.admin.dto.other.FileDto;
import com.dg.cuckoo.admin.repository.EarlyProjectRepository;
import com.dg.cuckoo.admin.repository.FileRepository;
import com.dg.cuckoo.admin.service.DevelopmentBookService;
import com.dg.cuckoo.admin.service.FileService;
import com.dg.cuckoo.admin.service.ProjectInfoServer;
import com.dg.cuckoo.admin.vo.PageDto;
import com.dg.cuckoo.common.utils.PageModel;
import com.dg.cuckoo.core.ServiceException;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class DevelopmentBookServiceimpl implements DevelopmentBookService {

    @Autowired
    private EarlyProjectRepository repository;
    @Autowired
    private ProjectInfoServer projectInfoServer;
    @Autowired
    private FileRepository fileRepository;
    @Autowired
    private FileService fileService;


    @Override
    public void save(String projectId, EarlyProjectDto dto, MultipartFile[] files) throws Exception {
        powerfulSave(projectId,dto,files);
    }

    @Override
    public void delect(String id) {
        if(id==null){
            throw new ServiceException("请选择行");
        }
        repository.deleteById(id);
        fileRepository.deleteAll(fileRepository.findByByMark(id));
    }

    @Override
    public String openContentInfo(String id) {
        if(id==null){
            throw new ServiceException("请选择行");
        }
        return repository.findById(id).get().getContentBox();
    }

    @Override
    public String openInfo(String id) {
        EarlyProjectDto dto = repository.findById(id).get();
        List<String> files = fileService.byByMark(id);
        JSONObject json = new JSONObject();
        json.put("dto",dto);
        json.put("files",files);
        return json.toString();
    }

    @Transactional
    @Override
    public void update(String id, String projectId, MultipartFile[] files, EarlyProjectDto dto) throws Exception {
        powerfulUpdate(id,projectId,files,dto);
    }

    @Override
    public PageModel page(Integer page, Integer pageSize,String projectId) {
        return powerfulPage(page,pageSize,projectId,1);
    }

    @Override
    public void powerfulSave(String projectId, EarlyProjectDto dto, MultipartFile[] files) throws Exception {
        if(projectId==null){
            throw new ServiceException("请选择项目");
        }
        checkSave(dto);
        dto.setProjectId(projectId);;
        EarlyProjectDto save = repository.save(dto);
        if (files!=null){
            if(files.length>0){
                projectInfoServer.powerfulFileSave(files,save.getId());
            }
        }
    }

    @Override
    public PageModel powerfulPage(Integer page, Integer pageSize, String projectId,Integer type) {
        Integer one = page;
        page = (page - 1) * pageSize;
        List<PageDto> pageDtos=new ArrayList<>();
        List<EarlyProjectDto> all = repository.findByProjectIdAndType(projectId,type);
        for(EarlyProjectDto e:all){
            List<FileDto> files = fileRepository.findByByMark(e.getId());
            List<String> newFiles=new ArrayList<>();
            for(FileDto f:files){
                newFiles.add(f.getFileUrl());
            }
            pageDtos.add(new PageDto<>(newFiles,e));
        }
        List<PageDto> data = pageDtos.stream().skip(page).limit(pageSize).collect(Collectors.toList());
        return new PageModel(one,pageSize,all.size(),data);
    }

    @Override
    public void powerfulUpdate(String id, String projectId, MultipartFile[] files, EarlyProjectDto dto) throws Exception {
        if(id==null){
            throw new ServiceException("请选择行");
        }
        checkSave(dto);
        dto.setId(id);
        dto.setProjectId(projectId);
        EarlyProjectDto save = repository.save(dto);
        fileRepository.deleteAll(fileRepository.findByByMark(id));
        if (files!=null){
        if(files.length>0){
            projectInfoServer.powerfulFileSave(files,save.getId());
        }
        }
    }

    public void checkSave(EarlyProjectDto dto){

    }
}
